class Main{
    constructor(){
        this.all = ['哥釉青花','孔雀绿青花','豆青釉青花','青花五彩','青花红彩','黄地青花'];
        this.choise = [];
        this.answer = [];
        this.name = '';
        this.time = 0;
        this.score = '';
        this.timer = null;
    }

    init(){
        while(!this.name){
            this.name = prompt('请输入你的名字：');
            $('.name').text(this.name);
        }
        while(this.all.length > 0){
            let num = Math.floor(Math.random() * this.all.length);
            let val = this.all.splice(num,1);
            this.choise.push(val[0]);
            let src = `${val[0]}/${Math.floor(Math.random() * 3)+1}.jpg`;
            $('.group li').eq(6 - this.choise.length).children('img').attr({src});
        }
        this.timer = setInterval(()=>{
            this.time++;
            $('.time').text(this.time);
        },1000);
    }

    confirm(val, parent){
        if(!val){
            alert('请在填写答案后提交！');
            return false;
        }
        this.answer.push(val);
        $(parent).animate({'left':'-500px'},200,'swing');
        this.calcScore();
    }

    calcScore(){
        this.score = 0;
        for(let i = 0; i < this.answer.length; i++){
            this.score = this.answer[i] == this.choise[i] ? ++this.score : this.score;
            if(i == 5){
                $.ajax({
                    url: './php/save.php',
                    type: 'POST',
                    data: {
                        name: this.name,
                        time: this.time,
                        score: this.score
                    }
                });
                clearInterval(this.timer);
            }
        }
        $('.score').text(this.score);
    }
}

var init = new Main();
init.init();

$('.btn').click(function(){
    init.confirm( $(this).siblings('input').val(), $(this).parents('li') );
});